<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.13.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CAD_Doc: gMock Cheat Sheet</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="text/javascript" src="clipboard.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
  $(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr id="projectrow">
  <td id="projectalign">
   <div id="projectname">CAD_Doc<span id="projectnumber">&#160;1.0</span>
   </div>
   <div id="projectbrief">cad synopsis</div>
  </td>
    <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <span id="MSearchSelect"                onmouseover="return searchBox.OnSearchSelectShow()"                onmouseout="return searchBox.OnSearchSelectHide()">&#160;</span>
          <input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.13.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() { codefold.init(0); });
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function(){initNavTree('md__build_2__deps_2googletest-src_2docs_2gmock__cheat__sheet.html',''); initResizable(true); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>

<div><div class="header">
  <div class="headertitle"><div class="title">gMock Cheat Sheet</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="autotoc_md111"></a></p>
<h1><a class="anchor" id="autotoc_md112"></a>
Defining a Mock Class</h1>
<h2><a class="anchor" id="MockClass"></a>
Mocking a Normal Class</h2>
<p>Given</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>Foo {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keyword">virtual</span> ~Foo();</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">int</span> GetSize() <span class="keyword">const</span> = 0;</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">string</span> Describe(<span class="keyword">const</span> <span class="keywordtype">char</span>* name) = 0;</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">string</span> Describe(<span class="keywordtype">int</span> type) = 0;</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">bool</span> Process(Bar elem, <span class="keywordtype">int</span> count) = 0;</div>
<div class="line">};</div>
</div><!-- fragment --><p>(note that <code>~Foo()</code> <b>must</b> be virtual) we can define its mock as</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;gmock/gmock.h&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">class </span>MockFoo : <span class="keyword">public</span> Foo {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">int</span>, GetSize, (), (<span class="keyword">const</span>, <span class="keyword">override</span>));</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">string</span>, Describe, (<span class="keyword">const</span> <span class="keywordtype">char</span>* name), (<span class="keyword">override</span>));</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">string</span>, Describe, (<span class="keywordtype">int</span> type), (<span class="keyword">override</span>));</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">bool</span>, Process, (Bar elem, <span class="keywordtype">int</span> count), (<span class="keyword">override</span>));</div>
<div class="line">};</div>
</div><!-- fragment --><p>To create a "nice" mock, which ignores all uninteresting calls, a "naggy" mock, which warns on all uninteresting calls, or a "strict" mock, which treats them as failures:</p>
<div class="fragment"><div class="line"><span class="keyword">using </span>::<a class="code hl_class" href="classtesting_1_1_nice_mock.html">testing::NiceMock</a>;</div>
<div class="line"><span class="keyword">using </span>::<a class="code hl_class" href="classtesting_1_1_naggy_mock.html">testing::NaggyMock</a>;</div>
<div class="line"><span class="keyword">using </span>::<a class="code hl_class" href="classtesting_1_1_strict_mock.html">testing::StrictMock</a>;</div>
<div class="line"> </div>
<div class="line">NiceMock&lt;MockFoo&gt; nice_foo;      <span class="comment">// The type is a subclass of MockFoo.</span></div>
<div class="line"><a class="code hl_class" href="class_naggy_mock.html">NaggyMock&lt;MockFoo&gt;</a> naggy_foo;    <span class="comment">// The type is a subclass of MockFoo.</span></div>
<div class="line">StrictMock&lt;MockFoo&gt; strict_foo;  <span class="comment">// The type is a subclass of MockFoo.</span></div>
<div class="ttc" id="aclass_naggy_mock_html"><div class="ttname"><a href="class_naggy_mock.html">NaggyMock</a></div><div class="ttdef"><b>Definition</b> gmock-nice-strict.h:192</div></div>
<div class="ttc" id="aclasstesting_1_1_naggy_mock_html"><div class="ttname"><a href="classtesting_1_1_naggy_mock.html">testing::NaggyMock</a></div><div class="ttdef"><b>Definition</b> gmock-nice-strict.h:192</div></div>
<div class="ttc" id="aclasstesting_1_1_nice_mock_html"><div class="ttname"><a href="classtesting_1_1_nice_mock.html">testing::NiceMock</a></div><div class="ttdef"><b>Definition</b> gmock-nice-strict.h:151</div></div>
<div class="ttc" id="aclasstesting_1_1_strict_mock_html"><div class="ttname"><a href="classtesting_1_1_strict_mock.html">testing::StrictMock</a></div><div class="ttdef"><b>Definition</b> gmock-nice-strict.h:234</div></div>
</div><!-- fragment --><p>{: .callout .note} <b>Note:</b> A mock object is currently naggy by default. We may make it nice by default in the future.</p>
<h2><a class="anchor" id="MockTemplate"></a>
Mocking a Class Template</h2>
<p>Class templates can be mocked just like any class.</p>
<p>To mock</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Elem&gt;</div>
<div class="line"><span class="keyword">class </span>StackInterface {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  <span class="keyword">virtual</span> ~StackInterface();</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">int</span> GetSize() <span class="keyword">const</span> = 0;</div>
<div class="line">  <span class="keyword">virtual</span> <span class="keywordtype">void</span> Push(<span class="keyword">const</span> Elem&amp; x) = 0;</div>
<div class="line">};</div>
</div><!-- fragment --><p>(note that all member functions that are mocked, including <code>~StackInterface()</code> <b>must</b> be virtual).</p>
<div class="fragment"><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Elem&gt;</div>
<div class="line"><span class="keyword">class </span>MockStack : <span class="keyword">public</span> StackInterface&lt;Elem&gt; {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">int</span>, GetSize, (), (<span class="keyword">const</span>, <span class="keyword">override</span>));</div>
<div class="line">  MOCK_METHOD(<span class="keywordtype">void</span>, Push, (<span class="keyword">const</span> Elem&amp; x), (<span class="keyword">override</span>));</div>
<div class="line">};</div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md113"></a>
Specifying Calling Conventions for Mock Functions</h2>
<p>If your mock function doesn't use the default calling convention, you can specify it by adding <code>Calltype(convention)</code> to <code>MOCK_METHOD</code>'s 4th parameter. For example,</p>
<div class="fragment"><div class="line">MOCK_METHOD(<span class="keywordtype">bool</span>, Foo, (<span class="keywordtype">int</span> n), (Calltype(STDMETHODCALLTYPE)));</div>
<div class="line">MOCK_METHOD(<span class="keywordtype">int</span>, Bar, (<span class="keywordtype">double</span> x, <span class="keywordtype">double</span> y),</div>
<div class="line">            (<span class="keyword">const</span>, Calltype(STDMETHODCALLTYPE)));</div>
</div><!-- fragment --><p>where <code>STDMETHODCALLTYPE</code> is defined by <code>&lt;objbase.h&gt;</code> on Windows.</p>
<h1><a class="anchor" id="UsingMocks"></a>
Using Mocks in Tests</h1>
<p>The typical work flow is:</p>
<ol type="1">
<li>Import the gMock names you need to use. All gMock symbols are in the <code>testing</code> namespace unless they are macros or otherwise noted.</li>
<li>Create the mock objects.</li>
<li>Optionally, set the default actions of the mock objects.</li>
<li>Set your expectations on the mock objects (How will they be called? What will they do?).</li>
<li>Exercise code that uses the mock objects; if necessary, check the result using googletest assertions.</li>
<li>When a mock object is destructed, gMock automatically verifies that all expectations on it have been satisfied.</li>
</ol>
<p>Here's an example:</p>
<div class="fragment"><div class="line"><span class="keyword">using </span>::testing::Return;                          <span class="comment">// #1</span></div>
<div class="line"> </div>
<div class="line">TEST(BarTest, DoesThis) {</div>
<div class="line">  MockFoo foo;                                    <span class="comment">// #2</span></div>
<div class="line"> </div>
<div class="line">  ON_CALL(foo, GetSize())                         <span class="comment">// #3</span></div>
<div class="line">      .WillByDefault(Return(1));</div>
<div class="line">  <span class="comment">// ... other default actions ...</span></div>
<div class="line"> </div>
<div class="line">  EXPECT_CALL(foo, Describe(5))                   <span class="comment">// #4</span></div>
<div class="line">      .Times(3)</div>
<div class="line">      .WillRepeatedly(Return(<span class="stringliteral">&quot;Category 5&quot;</span>));</div>
<div class="line">  <span class="comment">// ... other expectations ...</span></div>
<div class="line"> </div>
<div class="line">  EXPECT_EQ(MyProductionFunction(&amp;foo), <span class="stringliteral">&quot;good&quot;</span>);  <span class="comment">// #5</span></div>
<div class="line">}                                                 <span class="comment">// #6</span></div>
</div><!-- fragment --><h1><a class="anchor" id="OnCall"></a>
Setting Default Actions</h1>
<p>gMock has a <b>built-in default action</b> for any function that returns <code>void</code>, <code>bool</code>, a numeric value, or a pointer. In C++11, it will additionally returns the default-constructed value, if one exists for the given type.</p>
<p>To customize the default action for functions with return type <code>T</code>, use <a href="reference/mocking.md#DefaultValue"><code>DefaultValue&lt;T&gt;</code></a>. For example:</p>
<div class="fragment"><div class="line"><span class="comment">// Sets the default action for return type std::unique_ptr&lt;Buzz&gt; to</span></div>
<div class="line"><span class="comment">// creating a new Buzz every time.</span></div>
<div class="line">DefaultValue&lt;std::unique_ptr&lt;Buzz&gt;&gt;::SetFactory(</div>
<div class="line">    [] { <span class="keywordflow">return</span> MakeUnique&lt;Buzz&gt;(AccessLevel::kInternal); });</div>
<div class="line"> </div>
<div class="line"><span class="comment">// When this fires, the default action of MakeBuzz() will run, which</span></div>
<div class="line"><span class="comment">// will return a new Buzz object.</span></div>
<div class="line">EXPECT_CALL(mock_buzzer_, MakeBuzz(<span class="stringliteral">&quot;hello&quot;</span>)).Times(AnyNumber());</div>
<div class="line"> </div>
<div class="line"><span class="keyword">auto</span> buzz1 = mock_buzzer_.MakeBuzz(<span class="stringliteral">&quot;hello&quot;</span>);</div>
<div class="line"><span class="keyword">auto</span> buzz2 = mock_buzzer_.MakeBuzz(<span class="stringliteral">&quot;hello&quot;</span>);</div>
<div class="line">EXPECT_NE(buzz1, <span class="keyword">nullptr</span>);</div>
<div class="line">EXPECT_NE(buzz2, <span class="keyword">nullptr</span>);</div>
<div class="line">EXPECT_NE(buzz1, buzz2);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Resets the default action for return type std::unique_ptr&lt;Buzz&gt;,</span></div>
<div class="line"><span class="comment">// to avoid interfere with other tests.</span></div>
<div class="line">DefaultValue&lt;std::unique_ptr&lt;Buzz&gt;&gt;::Clear();</div>
</div><!-- fragment --><p>To customize the default action for a particular method of a specific mock object, use <a href="reference/mocking.md#ON_CALL"><code>ON_CALL</code></a>. <code>ON_CALL</code> has a similar syntax to <code>EXPECT_CALL</code>, but it is used for setting default behaviors when you do not require that the mock method is called. See <a href="gmock_cook_book.md#UseOnCall">Knowing When to Expect</a> for a more detailed discussion.</p>
<h1><a class="anchor" id="ExpectCall"></a>
Setting Expectations</h1>
<p>See <a href="reference/mocking.md#EXPECT_CALL"><code>EXPECT_CALL</code></a> in the Mocking Reference.</p>
<h1><a class="anchor" id="MatcherList"></a>
Matchers</h1>
<p>See the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2matchers.html">Matchers Reference</a>.</p>
<h1><a class="anchor" id="ActionList"></a>
Actions</h1>
<p>See the <a class="el" href="md__build_2__deps_2googletest-src_2docs_2reference_2actions.html">Actions Reference</a>.</p>
<h1><a class="anchor" id="CardinalityList"></a>
Cardinalities</h1>
<p>See the <a href="reference/mocking.md#EXPECT_CALL.Times"><code>Times</code> clause</a> of <code>EXPECT_CALL</code> in the Mocking Reference.</p>
<h1><a class="anchor" id="autotoc_md114"></a>
Expectation Order</h1>
<p>By default, expectations can be matched in <em>any</em> order. If some or all expectations must be matched in a given order, you can use the <a href="reference/mocking.md#EXPECT_CALL.After"><code>After</code> clause</a> or <a href="reference/mocking.md#EXPECT_CALL.InSequence"><code>InSequence</code> clause</a> of <code>EXPECT_CALL</code>, or use an <a href="reference/mocking.md#InSequence"><code>InSequence</code> object</a>.</p>
<h1><a class="anchor" id="autotoc_md115"></a>
Verifying and Resetting a Mock</h1>
<p>gMock will verify the expectations on a mock object when it is destructed, or you can do it earlier:</p>
<div class="fragment"><div class="line"><span class="keyword">using </span>::testing::Mock;</div>
<div class="line">...</div>
<div class="line"><span class="comment">// Verifies and removes the expectations on mock_obj;</span></div>
<div class="line"><span class="comment">// returns true if and only if successful.</span></div>
<div class="line">Mock::VerifyAndClearExpectations(&amp;mock_obj);</div>
<div class="line">...</div>
<div class="line"><span class="comment">// Verifies and removes the expectations on mock_obj;</span></div>
<div class="line"><span class="comment">// also removes the default actions set by ON_CALL();</span></div>
<div class="line"><span class="comment">// returns true if and only if successful.</span></div>
<div class="line">Mock::VerifyAndClear(&amp;mock_obj);</div>
</div><!-- fragment --><p>Do not set new expectations after verifying and clearing a mock after its use. Setting expectations after code that exercises the mock has undefined behavior. See <a href="gmock_for_dummies.md#using-mocks-in-tests">Using Mocks in Tests</a> for more information.</p>
<p>You can also tell gMock that a mock object can be leaked and doesn't need to be verified:</p>
<div class="fragment"><div class="line">Mock::AllowLeak(&amp;mock_obj);</div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md116"></a>
Mock Classes</h1>
<p>gMock defines a convenient mock class template</p>
<div class="fragment"><div class="line"><span class="keyword">class </span>MockFunction&lt;R(A1, ..., An)&gt; {</div>
<div class="line"> <span class="keyword">public</span>:</div>
<div class="line">  MOCK_METHOD(R, Call, (A1, ..., An));</div>
<div class="line">};</div>
</div><!-- fragment --><p>See this <a href="gmock_cook_book.md#UsingCheckPoints">recipe</a> for one application of it.</p>
<h1><a class="anchor" id="autotoc_md117"></a>
Flags</h1>
<table class="markdownTable">
<tr class="markdownTableHead">
<th class="markdownTableHeadLeft">Flag   </th><th class="markdownTableHeadLeft">Description    </th></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyLeft"><code>--gmock_catch_leaked_mocks=0</code>   </td><td class="markdownTableBodyLeft">Don't report leaked mock objects as failures.    </td></tr>
<tr class="markdownTableRowEven">
<td class="markdownTableBodyLeft"><code>--gmock_verbose=LEVEL</code>   </td><td class="markdownTableBodyLeft">Sets the default verbosity level (<code>info</code>, <code>warning</code>, or <code>error</code>) of Google <a class="el" href="class_mock.html">Mock</a> messages.   </td></tr>
</table>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
  </ul>
</div>
</body>
</html>
